package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisVOService;
import cn.wolfcode.luowowo.cache.service.UserInfoVisitVOService;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.domain.UserInfoAttention;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.comment.service.IUserInfoAttentionService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/other")
public class OtherPersonalCenterController {
    @Reference
    private IDestinationService destinationService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private ITravelStatisVOService travelStatisVOService;
    @Reference
    private IUserInfoAttentionService userInfoAttentionService;
    @Reference
    private UserInfoVisitVOService infoVisitVO;
    @Reference
    private IScenicCommentService scenicCommentService;
    @RequestMapping("/home")
    @RequireLogin
    public String home(Model model, Long uid,@UserParam UserInfo userInfo){

        UserInfo user = userInfoService.get(uid);

        List<Travel> travels = null;
        travels = travelService.get2TravelByUserId(user.getId());
        model.addAttribute("count",travels.size());
        //从redis中获取数据
        for (Travel travel : travels) {
            TravelStatisVO travelStatisVO = travelStatisVOService.getTravelStatisVO(travel.getId());
            travel.setViewnum(travelStatisVO.getViewnum());
            travel.setSharenum(travelStatisVO.getSharenum());
            travel.setReplynum(travelStatisVO.getReplynum());
            travel.setFavornum(travelStatisVO.getFavornum());
        }
        if (travels.size()>2){
        travels=travels.subList(0, 2);}
        //共享用户信息
        model.addAttribute("userInfo",user);
        //共享游记信息
        model.addAttribute("travels",travels);
        //创建假数据
        /*UserInfoAttention attention = new UserInfoAttention();
        attention.setBeUserId(4L);
        attention.setUserId(9L);
        attention.setHeadUrl("/images/default.jpg");
        attention.setUsername("demon");
        attention.setState(1);
        userInfoAttentionService.save(attention);*/
        List<UserInfoAttention> list = userInfoAttentionService.findAllByUserId(user.getId());
        List<UserInfoAttention> fans = userInfoAttentionService.findAllBybeUserId(user.getId());
        model.addAttribute("fans",fans.size());
        model.addAttribute("list",list);
        model.addAttribute("size",list.size());
        model.addAttribute("user",userInfo);

        return "other/homepage";

    }

    @RequestMapping("/mytravelnotes")
    @RequireLogin
    public String mytravelnotes(Model model ,Long uid,@UserParam UserInfo userInfo){
//最近访问
        //访问增加
        //.increaseVisit(uid, 1);
        if (uid!=userInfo.getId()){
        infoVisitVO.increaseVisit(uid,1,userInfo);
        }
        //获取访问量
        int todayNum = infoVisitVO.getUserInfoVisiVO(uid).getTodayNum();
        int visitNnm = infoVisitVO.getUserInfoVisiVO(uid).getVisitNnm();
        List<UserInfo> beUser = infoVisitVO.getUserInfoVisiVO(uid).getBeUser();
        model.addAttribute("beUser",beUser);
        model.addAttribute("todayNum",todayNum);
        model.addAttribute("visitNnm",visitNnm);
        //共享用户的信息
        UserInfo user = userInfoService.get(uid);
        model.addAttribute("userInfo",user);

        List<Travel> list =  travelService.get2TravelByUserId(user.getId());
        //pageInfo
        //从redis中获取到当前用户所有游记的key
        int viewnum =0;
        int replynum = 0;
        for (Travel travel : list) {
            TravelStatisVO tr = travelStatisVOService.getTravelStatisVO(travel.getId());
            viewnum += tr.getViewnum();
            replynum+= tr.getReplynum();
        }
        model.addAttribute("viewnum",viewnum);
        model.addAttribute("replynum",replynum);
        model.addAttribute("list",list);
        model.addAttribute("count",list.size());
        model.addAttribute("user",userInfo);
        return "other/mytravelnotes";

    }

    //他的点评
    @RequestMapping("/review")
    @RequireLogin
    public String review(Model model ,Long uid,@UserParam UserInfo user){
        UserInfo userInfo = userInfoService.get(uid);

        List<ScenicComment> list = scenicCommentService.getFavoriteScenicByUserId(uid);
        int like=0;
        for (ScenicComment scenicComment : list) {
            int thumbupnum = scenicComment.getThumbupnum();
            like+=thumbupnum;
        }
        int size = list.size();

        model.addAttribute("user",userInfo);
        model.addAttribute("like",like);
        model.addAttribute("count",size);
        model.addAttribute("list",list);
        model.addAttribute("userinfo",user);


        return "other/review";

    }


    @RequestMapping("/travelcollection")
    @RequireLogin
    public String travelcollection(Model model , Long uid){
        UserInfo user = userInfoService.get(uid);
        model.addAttribute("user",user);
        //查询收藏的游记
        return "other/travelcollection";
    }
}
